package jejunu.androboys.QuickCall.Component;

import jejunu.androboys.QuickCall.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddressBookListAdapter extends BaseAdapter
{
	private Activity mAct;
	private int mListLayout;
	LayoutInflater inflater;
	int listCount;
	String[][] mBook;

	public AddressBookListAdapter(Activity act, int listLayout, String[][] book) 
	{
		mAct = act;
		mListLayout = listLayout;
		inflater = (LayoutInflater) mAct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBook=book;
		listCount=book.length;
		
	}
	
	@Override
	public int getCount() 
	{
		return listCount;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		if (convertView == null) 
		{
			convertView = inflater.inflate(mListLayout, parent, false);
		}
		
		final int sel_address=position;
		
		TextView tv_name=(TextView) convertView.findViewById(R.id.AddressListContents_tv_name);
		TextView tv_number=(TextView) convertView.findViewById(R.id.AddressListContents_tv_number);
		TextView tv_type=(TextView) convertView.findViewById(R.id.AddressListContents_tv_type);
		
		tv_name.setText(mBook[position][0]);
		tv_number.setText(mBook[position][1]);
		tv_type.setText(mBook[position][2]);
		
		LinearLayout list_contents=(LinearLayout) convertView.findViewById(R.id.AddressListContents_lout);
		
		list_contents.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{				
				Intent setting_act=mAct.getIntent();
				
				setting_act.putExtra("name", mBook[sel_address][0]);
				setting_act.putExtra("number", mBook[sel_address][1]);
				setting_act.putExtra("type", mBook[sel_address][2]);
				
				mAct.setResult(mAct.RESULT_OK, setting_act);
				
				mAct.finish();
			}
		});
		
		
		
		return convertView;
	}
}
